package com.synjones.pay.web.biz.recharge.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.synjones.core.util.AmtHelper;
import com.synjones.core.util.Charsets;
import com.synjones.pay.model.context.StatusInfo;
import com.synjones.pay.model.recharge.RechargeReqBean;
import com.synjones.pay.model.recharge.RechargeRespBean;
import com.synjones.pay.web.biz.client.HttpClientUtils;
import com.synjones.pay.web.biz.client.RestFulClient;
import static com.synjones.pay.web.biz.common.PageParams.TICKET;
import com.synjones.pay.web.biz.common.SystemCfg;
import com.synjones.pay.web.biz.common.WxResponseBean;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * desc 充值service
 * @author sxj
 */
@Service("rechargeService")
public class RechargeServiceImpl implements IRechargeService {

    private static final Logger logger = LoggerFactory.getLogger(RechargeServiceImpl.class);

    @Override
    public WxResponseBean getRechargeCardInfo(String ticket) {
        ticket = "5b938b2098caecc9dc049b2eef01dd4040eaafb9";
        Map<String, String> map = new HashMap<>();
        map.put(TICKET, ticket); 
//        String respJson = HttpClientUtils.httpPost(SystemCfg.ECARD_URL, map, Charsets.UTF_8.name());
        String respJson = "{\"errCode\":\"0000\",\"errMsg\":null,\"tipMsg\":null,\"data\":{\"cardInfos\":[{\"eaccInfos\":[{\"name\":\"test2\",\"type\":\"001\",\"balance\":\"3911.0\"},{\"name\":\"test1\",\"type\":\"000\",\"balance\":\"17000.0\"}],\"account\":\"101987\",\"name\":\"田少英\",\"unsettle_amount\":\"-128199.0\",\"db_balance\":\"173999.0\",\"lostflag\":\"0\",\"freezeflag\":\"0\",\"idflag\":\"0\",\"barflag\":null,\"createdate\":\"2015-04-12T00:00:00\",\"expdate\":\"20350627\",\"cardtype\":\"800\",\"cardname\":\"正式卡\",\"bankacc\":\"\",\"sno\":\"03500326\",\"phone\":\"\",\"certtype\":\"001\",\"cert\":\"140103680911425\",\"autotrans_limite\":\"1000.0\",\"autotrans_amt\":\"2000.0\",\"autotrans_flag\":\"0\",\"mscard\":\"0\",\"scard_num\":\"0\"},{\"eaccInfos\":null,\"account\":\"6046\",\"name\":\"田少英\",\"unsettle_amount\":\"-280.0\",\"db_balance\":\"280.0\",\"lostflag\":\"1\",\"freezeflag\":\"0\",\"idflag\":\"0\",\"barflag\":null,\"createdate\":\"2002-08-28T00:00:00\",\"expdate\":\"20200716\",\"cardtype\":\"800\",\"cardname\":\"正式卡\",\"bankacc\":\"9558803602124739855\",\"sno\":\"03500326\",\"phone\":\"\",\"certtype\":\"001\",\"cert\":\"140103680911425\",\"autotrans_limite\":\"2000.0\",\"autotrans_amt\":\"5000.0\",\"autotrans_flag\":\"2\",\"mscard\":\"0\",\"scard_num\":\"0\"},{\"eaccInfos\":null,\"account\":\"60461111\",\"name\":\"田少英\",\"unsettle_amount\":\"109220.0\",\"db_balance\":\"280.0\",\"lostflag\":\"0\",\"freezeflag\":\"0\",\"idflag\":\"0\",\"barflag\":null,\"createdate\":\"2002-08-28T00:00:00\",\"expdate\":\"20200716\",\"cardtype\":\"800\",\"cardname\":\"正式卡\",\"bankacc\":\"9558803602124739855\",\"sno\":\"03500326\",\"phone\":\"\",\"certtype\":\"001\",\"cert\":\"140103680911425\",\"autotrans_limite\":\"2000.0\",\"autotrans_amt\":\"5000.0\",\"autotrans_flag\":\"2\",\"mscard\":\"0\",\"scard_num\":\"0\"},{\"eaccInfos\":null,\"account\":\"60462222\",\"name\":\"田少英\",\"unsettle_amount\":\"44720.0\",\"db_balance\":\"280.0\",\"lostflag\":\"0\",\"freezeflag\":\"0\",\"idflag\":\"0\",\"barflag\":null,\"createdate\":\"2002-08-28T00:00:00\",\"expdate\":\"20200716\",\"cardtype\":\"800\",\"cardname\":\"正式卡\",\"bankacc\":\"9558803602124739855\",\"sno\":\"03500326\",\"phone\":\"\",\"certtype\":\"001\",\"cert\":\"140103680911425\",\"autotrans_limite\":\"2000.0\",\"autotrans_amt\":\"5000.0\",\"autotrans_flag\":\"2\",\"mscard\":\"0\",\"scard_num\":\"0\"},{\"eaccInfos\":null,\"account\":\"60463333\",\"name\":\"田少英\",\"unsettle_amount\":\"131720.0\",\"db_balance\":\"280.0\",\"lostflag\":\"0\",\"freezeflag\":\"0\",\"idflag\":\"0\",\"barflag\":null,\"createdate\":\"2002-08-28T00:00:00\",\"expdate\":\"20200716\",\"cardtype\":\"800\",\"cardname\":\"正式卡\",\"bankacc\":\"9558803602124739855\",\"sno\":\"03500326\",\"phone\":\"\",\"certtype\":\"001\",\"cert\":\"140103680911425\",\"autotrans_limite\":\"2000.0\",\"autotrans_amt\":\"5000.0\",\"autotrans_flag\":\"2\",\"mscard\":\"0\",\"scard_num\":\"0\"},{\"eaccInfos\":null,\"account\":\"60464444\",\"name\":\"田少英\",\"unsettle_amount\":\"-280.0\",\"db_balance\":\"280.0\",\"lostflag\":\"0\",\"freezeflag\":\"0\",\"idflag\":\"0\",\"barflag\":null,\"createdate\":\"2002-08-28T00:00:00\",\"expdate\":\"20200716\",\"cardtype\":\"800\",\"cardname\":\"正式卡\",\"bankacc\":\"9558803602124739855\",\"sno\":\"03500326\",\"phone\":\"\",\"certtype\":\"001\",\"cert\":\"140103680911425\",\"autotrans_limite\":\"2000.0\",\"autotrans_amt\":\"5000.0\",\"autotrans_flag\":\"2\",\"mscard\":\"0\",\"scard_num\":\"0\"},{\"eaccInfos\":null,\"account\":\"101381\",\"name\":\"田少英\",\"unsettle_amount\":\"0.0\",\"db_balance\":\"0.0\",\"lostflag\":\"0\",\"freezeflag\":\"0\",\"idflag\":\"0\",\"barflag\":null,\"createdate\":\"2015-04-12T00:00:00\",\"expdate\":\"20350423\",\"cardtype\":\"800\",\"cardname\":\"正式卡\",\"bankacc\":\"\",\"sno\":\"03500326\",\"phone\":\"\",\"certtype\":\"001\",\"cert\":\"140103680911425\",\"autotrans_limite\":\"2000.0\",\"autotrans_amt\":\"5000.0\",\"autotrans_flag\":\"1\",\"mscard\":\"0\",\"scard_num\":\"0\"},{\"eaccInfos\":[{\"name\":\"test1\",\"type\":\"000\",\"balance\":\"50000.0\"},{\"name\":\"test6\",\"type\":\"005\",\"balance\":\"0.0\"},{\"name\":\"test2\",\"type\":\"001\",\"balance\":\"0.0\"}],\"account\":\"30\",\"name\":\"田少英\",\"unsettle_amount\":\"-106435.0\",\"db_balance\":\"106435.0\",\"lostflag\":\"0\",\"freezeflag\":\"1\",\"idflag\":\"0\",\"barflag\":null,\"createdate\":\"2015-04-12T00:00:00\",\"expdate\":\"20351123\",\"cardtype\":\"800\",\"cardname\":\"正式卡\",\"bankacc\":\"\",\"sno\":\"03500326\",\"phone\":\"\",\"certtype\":\"001\",\"cert\":\"140103680911425\",\"autotrans_limite\":\"2000.0\",\"autotrans_amt\":\"5000.0\",\"autotrans_flag\":\"1\",\"mscard\":\"0\",\"scard_num\":\"0\"},{\"eaccInfos\":null,\"account\":\"10138222\",\"name\":\"田少英\",\"unsettle_amount\":\"0.0\",\"db_balance\":\"0.0\",\"lostflag\":\"0\",\"freezeflag\":\"0\",\"idflag\":\"0\",\"barflag\":null,\"createdate\":\"2015-04-12T00:00:00\",\"expdate\":\"20350423\",\"cardtype\":\"800\",\"cardname\":\"正式卡\",\"bankacc\":\"\",\"sno\":\"03500326\",\"phone\":\"\",\"certtype\":\"001\",\"cert\":\"140103680911425\",\"autotrans_limite\":\"2000.0\",\"autotrans_amt\":\"5000.0\",\"autotrans_flag\":\"1\",\"mscard\":\"0\",\"scard_num\":\"0\"},{\"eaccInfos\":null,\"account\":\"10138333\",\"name\":\"田少英\",\"unsettle_amount\":\"0.0\",\"db_balance\":\"0.0\",\"lostflag\":\"0\",\"freezeflag\":\"0\",\"idflag\":\"0\",\"barflag\":null,\"createdate\":\"2015-04-12T00:00:00\",\"expdate\":\"20350423\",\"cardtype\":\"800\",\"cardname\":\"正式卡\",\"bankacc\":\"\",\"sno\":\"03500326\",\"phone\":\"\",\"certtype\":\"001\",\"cert\":\"140103680911425\",\"autotrans_limite\":\"2000.0\",\"autotrans_amt\":\"5000.0\",\"autotrans_flag\":\"1\",\"mscard\":\"0\",\"scard_num\":\"0\"}],\"userInfo\":{\"account\":\"03500326\",\"sex\":\"2\",\"name\":\"田少英\",\"schoolCode\":\"wx\",\"schoolName\":\"微校园\"}},\"time\":null}";
        logger.info("充值返回用户信息：\r\n【{}】", respJson);
        WxResponseBean respBean = JSON.parseObject(respJson, WxResponseBean.class);
        return WxResponseBean.assemErrorWxRespBean(respBean);//httpPost 出错时，添加错误信息
    }

    @Override
    public RechargeRespBean getRechargeRespBean(RechargeReqBean rrb) {
        rrb.setAmount(String.valueOf(AmtHelper.yuanToPoint(rrb.getAmount())));//转账前，需要将转账金额转换成 分 为单位
        String jsonParam = JSON.toJSONString(rrb, SerializerFeature.WriteMapNullValue, SerializerFeature.SortField);
        String httpPost = RestFulClient.httpPost(SystemCfg.RECHARGE_PAY_URL, jsonParam, Charsets.UTF_8.name());
//        String httpPost = "{\"statusCode\":\"0000\",\"statusDesc\":\"seccess\",\"appId\":null,\"clientType\":\"2\",\"device\":\"Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1\",\"ucode\":\"wx\",\"uname\":\"微校园\",\"amount\":\"5000\",\"remark\":\"电子账户充值\",\"rechargeAcc\":\"001\",\"accName\":\"test2\",\"orderId\":\"RG201706061115531000000000\",\"completeTime\":null,\"serialNo\":null,\"paytoolCode\":null,\"paytoolName\":null,\"respMap\":{\"jumpUrl\":\"http://pay.dlmu.edu.cn/pay/dealPay.html?pwd=42B068F48B2AC0E0\"}}";
        logger.info("充值接口【{}】返回数据为【{}】", SystemCfg.RECHARGE_PAY_URL, httpPost);
        RechargeRespBean rechargeRespBean = JSON.parseObject(httpPost, RechargeRespBean.class);
        return assemFailRechargeRespBean(rechargeRespBean);
    }

    private static RechargeRespBean assemFailRechargeRespBean(RechargeRespBean rechargeRespBean) {
        if (null == rechargeRespBean) {
            rechargeRespBean = new RechargeRespBean();
            rechargeRespBean.setStatusCode(StatusInfo._9000.code());
            rechargeRespBean.setStatusDesc("网络忙，请稍后重试。");
        }else{//将金额单位转为元
            rechargeRespBean.setAmount(AmtHelper.poingToYuan(rechargeRespBean.getAmount()));//将返回的金额，转成元
        }
        return rechargeRespBean;
    }

    @Override
    public RechargeRespBean recRechargeRespBean(Map<String, String> map) {
        String httpPost = HttpClientUtils.httpPost(SystemCfg.RECHARGE_RECEIVE_RESULT_URL, map, Charsets.UTF_8.name());
        logger.info("充值接口接受返回结果【{}】返回数据为【{}】", SystemCfg.RECHARGE_RECEIVE_RESULT_URL, httpPost);
        RechargeRespBean rechargeRespBean = JSON.parseObject(httpPost, RechargeRespBean.class);
        return assemFailRechargeRespBean(rechargeRespBean);
    }

}
